package com.dykj.fireFighting.web.app;

import com.dykj.fireFighting.api.base.bigdata.rcxc.entity.PatrolReportRecord;
import com.dykj.fireFighting.api.base.bigdata.rcxc.service.IPatrolReportRecordService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.maintenance.dto.UrgentMaintenanceDto;
import com.dykj.fireFighting.api.base.maintenance.entity.UrgentMaintenance;
import com.dykj.fireFighting.api.base.maintenance.service.IRepairRecordService;
import com.dykj.fireFighting.api.base.maintenance.service.IUrgentMaintenanceService;
import com.dykj.fireFighting.api.base.maintenance.vo.MaintenanceEvaluationVo;
import com.dykj.fireFighting.api.base.maintenance.vo.UrgentMaintenanceVo;
import com.dykj.fireFighting.api.base.maintenanceReply.entity.MaintenanceReply;
import com.dykj.fireFighting.api.base.maintenanceReply.service.IMaintenanceReplyService;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 紧急维保记录接口
 *
 * @Author hxb
 * @Date 2021-07-05 10:48:41
 */
@Slf4j
@Api(value = "app紧急维保记录接口", tags = {"app紧急维保记录接口"})
@RestController
@RequestMapping("/app/urgenmaintenance")
public class UrgenmaintenanceController {

    @DubboReference
    private IUrgentMaintenanceService urgentMaintenanceService;
    @DubboReference
    private IRepairRecordService repairRecordService;
    @DubboReference
    private IPatrolReportRecordService patrolReportRecordService;
    @DubboReference
    private IMaintenanceReplyService maintenanceReplyService;
    @DubboReference
    private IEnterpriseService enterpriseService;

    /**
     * 紧急维保记录列表
     *
     * @return
     */
    @AutoLog(value = "紧急维保记录-分页列表查询")
    @ApiOperation(value = "紧急维保记录分页列表", notes = "紧急维保记录分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<UrgentMaintenanceVo>> treeGrid(UrgentMaintenanceDto urgentMaintenanceDto) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        urgentMaintenanceDto.setSociologyDept(loginUserVo.getEnterpriseId().toString());
        PageVo<UrgentMaintenanceVo> page = urgentMaintenanceService.queryUrgentList(urgentMaintenanceDto);
        return CommonResult.success(page);
    }

    /**
     * 获取回复表信息
     */
    @AutoLog(value = "回复表-获取回复表信息")
    @ApiOperation(value = "获取回复表信息", notes = "获取回复表信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/replyInfo/{id}")
    //@RequiresPermissions("app/maintenanceReply/info")
    public CommonResult<MaintenanceReply> replyInfo(@PathVariable("id") String id){
        MaintenanceReply maintenanceReply = maintenanceReplyService.getById(id);
        return CommonResult.success(maintenanceReply);
    }

    /**
     * 获取紧急维保记录信息
     */
    @AutoLog(value = "紧急维保记录-获取紧急维保记录信息")
    @ApiOperation(value = "获取紧急维保记录信息", notes = "获取紧急维保记录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<UrgentMaintenanceVo> info(@PathVariable("id") String id) {
        UrgentMaintenanceVo urgenmaintenanceVo = urgentMaintenanceService.getInfoById(id);
        return CommonResult.success(urgenmaintenanceVo);
    }

    /**
     * 保存紧急维保记录信息
     */
    @AutoLog(value = "紧急维保记录-保存紧急维保记录信息")
    @ApiOperation(value = "保存紧急维保记录信息", notes = "保存紧急维保记录信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody UrgentMaintenance urgentMaintenance) {
        try {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            urgentMaintenance.setId(UUIDUtils.get32UUID());
            urgentMaintenance.setSociologyDept(loginUserVo.getEnterpriseId().toString());
            urgentMaintenance.setCreateBy(loginUserVo.getId().toString());
            urgentMaintenance.setCreateDate(new Date());
            urgentMaintenanceService.save(urgentMaintenance);

            Enterprise enterprise=enterpriseService.getById(loginUserVo.getEnterpriseId());
            MaintenanceReply maintenanceReply=new MaintenanceReply();
            maintenanceReply.setId(UUIDUtils.get32UUID());
            maintenanceReply.setCreateBy(loginUserVo.getId().toString());
            maintenanceReply.setCreateTime(new Date());
            maintenanceReply.setRelationId(urgentMaintenance.getId());
            maintenanceReply.setContent(urgentMaintenance.getRemark());
            maintenanceReply.setUnitName(enterprise.getEnterpriseName());
            maintenanceReply.setType("1");
            maintenanceReplyService.save(maintenanceReply);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 批量保存紧急维保记录信息
     */
    @AutoLog(value = "批量保存紧急维保记录信息")
    @ApiOperation(value = "批量保存紧急维保记录信息", notes = "批量保存紧急维保记录信息")
    @PostMapping("/batchSave")
    public CommonResult batchSave(@Valid @RequestBody UrgentMaintenanceDto urgentMaintenanceDto) {
        try {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            List<PatrolReportRecord> patrolReportRecordList = urgentMaintenanceDto.getPatrolReportRecordList();
            List<String> list=new ArrayList<>();
            if (!CollectionUtils.isEmpty(patrolReportRecordList)) {
                for (PatrolReportRecord patrolReportRecord : patrolReportRecordList) {
                    UrgentMaintenance urgentMaintenance = new UrgentMaintenance();
                    BeanUtils.copyProperties(urgentMaintenanceDto, urgentMaintenance);
                    urgentMaintenance.setId(UUIDUtils.get32UUID());
                    urgentMaintenance.setDeviceId(patrolReportRecord.getDeviceId());
                    urgentMaintenance.setSociologyDept(loginUserVo.getEnterpriseId().toString());
                    urgentMaintenance.setCreateBy(loginUserVo.getId().toString());
                    urgentMaintenance.setCreateDate(new Date());
                    urgentMaintenanceService.save(urgentMaintenance);
                    list.add(urgentMaintenance.getId());

                    patrolReportRecordService.updatePatrolReportRecord(patrolReportRecord);
                }
            }
            String str= StringUtils.join(list, ",");
            Enterprise enterprise=enterpriseService.getById(loginUserVo.getEnterpriseId());
            MaintenanceReply maintenanceReply=new MaintenanceReply();
            maintenanceReply.setId(UUIDUtils.get32UUID());
            maintenanceReply.setCreateBy(loginUserVo.getId().toString());
            maintenanceReply.setCreateTime(new Date());
            maintenanceReply.setRelationId(str);
            maintenanceReply.setContent(urgentMaintenanceDto.getRemark());
            maintenanceReply.setUnitName(enterprise.getEnterpriseName());
            maintenanceReply.setType("1");
            maintenanceReplyService.save(maintenanceReply);

            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 维保评价
     */
    @AutoLog(value = "维保评价")
    @ApiOperation(value = "维保评价", notes = "维保评价")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody UrgentMaintenance urgentMaintenance) {
        try {
            urgentMaintenance.setEvaluationDate(new Date());
            urgentMaintenanceService.updateById(urgentMaintenance);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 维保评价列表
     */
    @AutoLog(value = "维保评价列表")
    @ApiOperation(value = "维保评价列表", notes = "维保评价列表")
    @GetMapping("/queryEvaluationList")
    public CommonResult<PageVo<UrgentMaintenanceVo>> queryEvaluationList(@Valid @RequestBody UrgentMaintenanceDto urgentMaintenanceDto) {
        if (ShiroUtil.getUser().getEnterpriseId() != null) {
            urgentMaintenanceDto.setMaintenanceDept(ShiroUtil.getUser().getEnterpriseId().toString());
        }
        PageVo<UrgentMaintenanceVo> page = urgentMaintenanceService.queryEvaluationList(urgentMaintenanceDto);
        return CommonResult.success(page);
    }


    /**
     * 维保服务机构的当月评价得分
     *
     * @param
     * @return
     */
    @AutoLog(value = "维保机构当月评价得分")
    @ApiOperation(value = "维保机构当月评价得分", notes = "维保机构当月评价得分")
    @GetMapping("/queryMaintenanceEvaluationList")
    public CommonResult<List<MaintenanceEvaluationVo>> queryMaintenanceEvaluationList() {
        String unitId = ShiroUtil.getUser().getEnterpriseId().toString();
        UrgentMaintenanceDto urgentMaintenanceDto = new UrgentMaintenanceDto();
        urgentMaintenanceDto.setSociologyDept(unitId);
        List<MaintenanceEvaluationVo> maintenanceEvaluationVoList = urgentMaintenanceService.queryMaintenanceEvaluationList(urgentMaintenanceDto);
        return CommonResult.success(maintenanceEvaluationVoList);
    }
}
